package team.sun.sss.controller.hardwork;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import team.sun.sss.dao.hardwork.HwApplyDao;
import team.sun.sss.dao.hardwork.HwBatchDao;
import team.sun.sss.dao.hardwork.HwLaborDao;
import team.sun.sss.dao.hardwork.HwTimeDao;
import team.sun.sss.dao.student.FamilyDao1;
import team.sun.sss.dao.student.StudentDao;

import team.sun.sss.pojo.hardwork.HwTime;
import team.sun.sss.pojo.user.User;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Controller
public class HwTimeController {
    @Autowired
    HwApplyDao hwApplyDao;
    @Autowired
    StudentDao studentDao;
    @Autowired
    FamilyDao1 familyDao1;
    @Autowired
    HwBatchDao hwBatchDao;
    @Autowired
    HwLaborDao hwLaborDao;
    @Autowired
    HwTimeDao hwTimeDao;
    //只查出某个学生的工时
    @RequestMapping("/hw_stu_time")
    public String stuTime(Model model, HttpSession session){
        User user = (User) session.getAttribute("loginInfo");
        model.addAttribute("HwTimeList",hwTimeDao.stuTime(user.getUserId()));
        return "hardWork/hw_stu_time";
    }
    @RequestMapping("/hw_staff_time")
    public String staffTime(Model model){
        //用人单位工时
        model.addAttribute("batchList",hwTimeDao.getbatchNum());
        model.addAttribute("HwTimeList",hwTimeDao.threeTableTime());
        return "hardWork/hw_staff_time";

    }
    @RequestMapping("staff_time_select")
    public String staff_stuApply_select(Model model,String batchTeam,String unit,Integer stuId,String stuName){
        model.addAttribute("batchList",hwTimeDao.getbatchNum());

        /*if(stuName!=null ){
            model.addAttribute("HwTimeList",hwTimeDao.selectByName(stuName));
        }else if( stuId!=null){
            model.addAttribute("HwTimeList",hwTimeDao.selectByStuId(stuId));

        }
        else{
            model.addAttribute("HwTimeList",hwTimeDao.selectByBatch(batchTeam,unit));
        }*/

        if(batchTeam.equals(0)&&unit.equals(1)&&(stuName!=null)&&(stuId==null)){
            model.addAttribute("HwTimeList",hwTimeDao.selectByName(stuName));
        }else if(stuId!=null){
            model.addAttribute("HwTimeList",hwTimeDao.selectByStuId(stuId));
        }else{
            model.addAttribute("HwTimeList",hwTimeDao.selectByBatch(batchTeam,unit));
        }

        return "hardWork/hw_staff_time";
    }
    @RequestMapping("/hw_staff_timeDetail/{num}")
    public String staffTimeDetail(@PathVariable("num") Integer num, Model model){
        model.addAttribute("HwTimeList",hwTimeDao.threeTableTimeDetail(num));

        return "hardWork/hw_staff_timeDetail";

    }

    @RequestMapping("/to_hw_staff_TimeUpdate/{num}")
    public String tostaffTimeUpdate(@PathVariable("num") Integer num, Model model){
        model.addAttribute("HwTimeList",hwTimeDao.threeTableTimeDetail(num));
        return "hardWork/hw_staff_timeUpdate";

    }
    @RequestMapping("/hw_staff_TimeUpdate")
    public String staffTimeUpdate( @ModelAttribute(value = "HwTimeList") HwTime hwTime,Integer num
            , Model model,String hwTimeApplyState){
        hwTimeDao.updateHwLaborState( num, hwTimeApplyState);
        model.addAttribute("HwTimeList",hwTimeDao.threeTableTime());
        return "hardWork/hw_staff_time";

    }
    @RequestMapping("/hw_unit_time")
    public String employerTime(Model model){
        model.addAttribute("HwTimeList",hwTimeDao.threeTableTime());
        return "hardWork/hw_unit_time";
    }
    @RequestMapping("/to_hw_unit_timeAdd")
    public String to_hw_unit_timeAdd(Model model){
        model.addAttribute("stuIdList",hwTimeDao.getStuIdNum());
        model.addAttribute("batchList",hwTimeDao.getbatchNum());
        return "hardWork/hw_unit_timeAdd";

    }
    @RequestMapping("/hw_unit_timeAdd")
    public String hw_unit_timeAdd(  Model model,String batchTeam,String laborUnit,Integer stuId,
                                    Integer year,Integer month,Integer hour,Float salary,Float allSalary){
        //用人单位申请表
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        String submitTime = today.format(fmt);
        hwTimeDao.addHwTime( stuId,laborUnit,batchTeam, year,month,hour,salary, allSalary, submitTime);
        model.addAttribute("HwTimeList",hwTimeDao.threeTableTime());
        return "hardWork/hw_unit_time";

    }
    @RequestMapping("/to_hw_unit_writeTime/{num}")
    public String toEmployerWriteTime(@PathVariable("num") Integer num, Model model){
        model.addAttribute("HwTimeList",hwTimeDao.threeTableTimeDetail(num));
        return "hardWork/hw_unit_timeWrite";

    }
    @RequestMapping("/hw_unit_writeTime")
    public String employerWriteTime( Integer num, Model model,Integer year,Integer month,Integer hour,Float salary,
                                     Float allSalary){
        //用人单位申请表
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        String submitTime = today.format(fmt);
        hwTimeDao.updateHwLabor( num, year,month,hour,salary, allSalary, submitTime);
        model.addAttribute("HwTimeList",hwTimeDao.threeTableTime());
        return "hardWork/hw_unit_time";

    }
}
